﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MMO_MANAGER.FORM_CATALOG;
using MMO_MANAGER.BUS;
using MMO_MANAGER.FORM_POPUP;
using MMO_MANAGER.DTO;
using MMO_MANAGER.Utils;
using SecurityMD5;
using System.Threading;

namespace MMO_MANAGER
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        private frmChangePass m_FrmDoiMatKhau;
        private frmLogon m_FrmLogin;
        private user_bus bus_us;
        private userlist obj_us;
        public frmMain()
        {
            Thread t = new Thread(new ThreadStart(splashScreen));
            t.Start();
            Thread.Sleep(3000);

            InitializeComponent();
            t.Abort();
            this.Text = "MMO MANAGERMENTS ENTERPRISE - HOGIAPHAT COMPANY - Version: " + Application.ProductVersion;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            bus_us = new user_bus();
            obj_us = new userlist();
            lbl_ver.Caption = "Version: " + Application.ProductVersion;
            lbl_date.EditValue = DateTime.Today;
            Default();
            DangNhap();
        }

        private void btn_email_list_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            scm_main.ShowWaitForm();
            _TabControl.EMAILLIST();
            scm_main.CloseWaitForm();
        }

        private void btn_ga_list_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            scm_main.ShowWaitForm();
            _TabControl.GALIST();
            scm_main.CloseWaitForm();
        }

        private void btn_key_word_list_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            scm_main.ShowWaitForm();
            _TabControl.KWLIST();
            scm_main.CloseWaitForm();
        }

        private void btn_gplus_list_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            scm_main.ShowWaitForm();
            _TabControl.GPLIST();
            scm_main.CloseWaitForm();
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            scm_main.ShowWaitForm();
            _TabControl.LBLIST();
            scm_main.CloseWaitForm();
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            scm_main.ShowWaitForm();
            _TabControl.CHANNELLIST();
            scm_main.CloseWaitForm();
        }

        private void btn_network_list_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            scm_main.ShowWaitForm();
            _TabControl.NETWORKLIST();
            scm_main.CloseWaitForm();
        }

        private void btn_logon_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (m_FrmLogin == null || m_FrmLogin.IsDisposed)
                m_FrmLogin = new frmLogon();
            m_FrmLogin.txtUsername.Text = "";
            m_FrmLogin.txtPassword.Text = "";
            m_FrmLogin.lbl_ErrorUser.Text = "";
            m_FrmLogin.lbl_Errorpass.Text = "";

            DangNhap();
        }

        private void btn_changepwd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (m_FrmDoiMatKhau == null || m_FrmDoiMatKhau.IsDisposed)
                m_FrmDoiMatKhau = new frmChangePass();

            m_FrmDoiMatKhau.txt_oldpass.Text = "";
            m_FrmDoiMatKhau.txt_newpass.Text = "";
            m_FrmDoiMatKhau.txt_newpass2.Text = "";
            m_FrmDoiMatKhau.lbl_oldpass.Text = "";
            m_FrmDoiMatKhau.lbl_newpass.Text = "";
            m_FrmDoiMatKhau.lbl_newpass2.Text = "";

            DoiMatKhau();
        }

        private void btn_logout_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // - Kiểm tra Form nào đang mở thì đóng lại
            foreach (Form frm in MdiChildren)
            {
                if (frm.ShowInTaskbar)
                    frm.Close();
            }
            m_FrmLogin.txtUsername.Clear();
            m_FrmLogin.txtPassword.Clear();
            m_FrmLogin.txtUsername.Focus();
            Default();
            DangNhap();
        }

        private void btn_exit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult dr = EasyDialog.ShowYesNoDialog("Are you sure exit ?");
            if (dr == DialogResult.Yes)
            {
                //log.WriteLine(LogType.exit, "Thoát phần mềm.");
                Application.Exit();
            }
        }


        #region Hàm Viết Thêm

        #region DangNhap
        public static string TenNguoiDung { get; set; }
        public void DangNhap()
        {
        Cont:
            if (m_FrmLogin == null || m_FrmLogin.IsDisposed)
                m_FrmLogin = new frmLogon();
            m_FrmLogin.txtUsername.Focus();

            if (m_FrmLogin.ShowDialog() == DialogResult.OK)
            {
                if (m_FrmLogin.Text == "")
                {
                    m_FrmLogin.lbl_Errorpass.Text = "";
                    m_FrmLogin.lbl_ErrorUser.Text = "You're not type Name!";
                    goto Cont;
                }

                if (m_FrmLogin.txtPassword.Text == "")
                {
                    m_FrmLogin.lbl_ErrorUser.Text = "";
                    m_FrmLogin.lbl_Errorpass.Text = "You're not type Password!";
                    goto Cont;
                }
                userlist nd = LayTT();
                var bl = new user_bus();
                int ketQua = bl.DangNhap(nd);
                switch (ketQua)
                {
                    case 0:
                        m_FrmLogin.lbl_Errorpass.Text = "";
                        m_FrmLogin.lbl_ErrorUser.Text = "This Name's not a valid!";
                        goto Cont;
                    case 1:
                        m_FrmLogin.lbl_ErrorUser.Text = "";
                        m_FrmLogin.lbl_Errorpass.Text = "This Password's not a valid!";
                        goto Cont;
                    case 2:
                        lbl_tenuser.Caption = "User: " + nd.tenht;
                        TenNguoiDung = nd.tenht;
                        Permissions(nd.role);
                        break;
                }
            }
            else
                return;
        }

        #endregion

        #region Phân quyền

        public void Permissions(int m_Per)
        {
            switch (m_Per)
            {
                case 1:
                    IsAdmin();
                    break;
                //case 2:
                //    IsManager();
                //    break;
                //case 3:
                //    IsKeToan();
                //    break;
                //case 4:
                //    IsUsers();
                //    break;
                //case 0:
                //    IsDisable();
                //    break;
                default:
                    Default();
                    break;
            }
        }

        #endregion

        #region DoiMatKhau

        public void DoiMatKhau()
        {
        Cont:
            if (m_FrmDoiMatKhau.ShowDialog() == DialogResult.OK)
            {
                if (m_FrmDoiMatKhau.txt_oldpass.Text == "")
                {
                    m_FrmDoiMatKhau.lbl_oldpass.Text = "You're not type pass old!";
                    m_FrmDoiMatKhau.lbl_newpass.Text = "";
                    m_FrmDoiMatKhau.lbl_newpass2.Text = "";
                    goto Cont;
                }

                if (m_FrmDoiMatKhau.txt_newpass.Text == "")
                {
                    m_FrmDoiMatKhau.lbl_oldpass.Text = "";
                    m_FrmDoiMatKhau.lbl_newpass.Text = "You're not type pass new!";
                    m_FrmDoiMatKhau.lbl_newpass2.Text = "";
                    goto Cont;
                }

                if (m_FrmDoiMatKhau.txt_newpass2.Text == "")
                {
                    m_FrmDoiMatKhau.lbl_oldpass.Text = "";
                    m_FrmDoiMatKhau.lbl_newpass.Text = "";
                    m_FrmDoiMatKhau.lbl_newpass2.Text = "You're not re-type pass new!";
                    goto Cont;
                }

                String m_Username = m_FrmLogin.txtUsername.Text;
                String m_Password = m_FrmLogin.txtPassword.Text;

                String m_OldPassword = m_FrmDoiMatKhau.txt_oldpass.Text;
                String m_NewPassword = m_FrmDoiMatKhau.txt_newpass.Text;
                String m_ReNPassword = m_FrmDoiMatKhau.txt_newpass2.Text;

                if (m_Password != m_OldPassword)
                {
                    m_FrmDoiMatKhau.lbl_oldpass.Text = "This Password's not a valid!";
                    m_FrmDoiMatKhau.lbl_newpass.Text = "";
                    m_FrmDoiMatKhau.lbl_newpass2.Text = "";
                    goto Cont;
                }
                else if (m_NewPassword != m_ReNPassword)
                {
                    m_FrmDoiMatKhau.lbl_oldpass.Text = "";
                    m_FrmDoiMatKhau.lbl_newpass.Text = "";
                    m_FrmDoiMatKhau.lbl_newpass2.Text = "This Re-Password's not a valid!";
                    goto Cont;
                }
                else
                {
                    userlist nd = LayTT();
                    nd.pwd = m_NewPassword.MaHoaMD5();
                    var bl = new user_bus();
                    bl.ChangePassword(nd);
                    EasyDialog.ShowSuccessfulDialog("Change password successfull!");
                }
            }
            else
                return;
        }

        #endregion

        #region Giao diện mặc định

        public void Default()
        {
            lbl_tenuser.Caption = "User:";
            btn_logon.Enabled = true;
            btn_changepwd.Enabled = false;
            btn_logout.Enabled = false;
            btn_exit.Enabled = true;

            btn_catalog.Enabled = false;
            btn_email_list.Enabled = false;
            btn_ga_list.Enabled = false;
            btn_key_word_list.Enabled = false;
            btn_gplus_list.Enabled = false;
            btn_label.Enabled = false;
            btn_network_list.Enabled = false;
            btn_channel.Enabled = false;
            
        }

        #endregion

        #region Giao diện khi đăng nhập với quyền Admin

        public void IsAdmin()
        {
            btn_logon.Enabled = false;
            btn_changepwd.Enabled = true;
            btn_logout.Enabled = true;
            btn_exit.Enabled = true;

            btn_catalog.Enabled = true;
            btn_email_list.Enabled = true;
            btn_ga_list.Enabled = true;
            btn_key_word_list.Enabled = true;
            btn_gplus_list.Enabled = true;
            btn_label.Enabled = true;
            btn_network_list.Enabled = true;
            btn_channel.Enabled = true;
        }

        #endregion

        #region Giao diện khi đăng nhập với quyền Manager

        public void IsManager()
        {
            
        }

        #endregion

        #region Giao diện khi đăng nhập với quyền Kế Toán

        public void IsKeToan()
        {
           
        }

        #endregion

        #region Giao diện khi đăng nhập với quyền Users

        public void IsUsers()
        {
            
        }

        #endregion

        #region Giao diện khi đăng nhập với quyền bị khóa

        public void IsDisable()
        {
            //Default();
            //XtraMessageBox.Show("Người dùng này đã bị khóa!");
            //DangNhap();
        }

        #endregion

        public static string MaNguoiDung { get; set; }
        private userlist LayTT()
        {
            var nd = new userlist();
            nd.tendn = m_FrmLogin.txtUsername.Text;
            //nd.UserPass = m_FrmLogin.txtPassword.Text;
            nd.pwd = m_FrmLogin.txtPassword.Text.MaHoaMD5();
            //nd
            MaNguoiDung = nd.tendn;
            return nd;
        }

        #endregion

        private void btn_help_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EasyDialog.ShowInfoDialog("Add: F2\nEdit: F3\nDelete: F4\nRefresh: F5\n\nApply for all form!");
        }

        private void splashScreen()
        {
            Application.Run(new Welcome());
        }

        private void btn_demo_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            key_word_bus buss = new key_word_bus();
            buss.Create();
        }

        private void btn_close_all_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult dr = EasyDialog.ShowYesNoDialog("Are you sure close all form display?");
            if (dr == DialogResult.Yes)
            {
                foreach (Form frm in MdiChildren)
                {
                    if (frm.IsMdiChild)
                        frm.Close();
                }
            }
        }

        private void btn_about_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EasyDialog.ShowInfoDialog("Author: MrDat");
        }


        
    }
}
